// ============================== 导入依赖 ============================== //

import CORE from '@root/【CORE】'

// ============================== 导出转换 ============================== //

export default {
  // —————————————————————————————— 转换提示 —————————————————————————————— //

  transTip(name) {
    return `【 __初始化__ > ${name} 】 查询${name}信息`
  },

  // —————————————————————————————— 转换全部字典 —————————————————————————————— //

  transAllDict(DICT, tree) {
    tree.forEach(({ dictTag, child }) => {
      if (child) {
        child.sort((a, b) => a.dictOrder - b.dictOrder)

        // 以 ID 进行翻译
        DICT[dictTag] = child.map((item) => [
          item.id, // 0 主键
          item.dictName, // 1 名称
          item.dictTag, // 2 编号
          item.dictAbout, // 3 备注
          item.parentId, // 4 父级
          () => item.dictStatus === 2, // 5 状态
        ])

        // 以 code 进行翻译
        DICT[`${dictTag}_code`] = child.map((item) => [
          item.dictTag, // 0 编号
          item.dictName, // 1 名称
          item.id, // 2 主键
          item.dictAbout, // 3 备注
          item.parentId, // 4 父级
          () => item.dictStatus === 2, // 5 状态
        ])
        this.transAllDict(DICT, child)
      }
    })
    return DICT
  },

  // —————————————————————————————— 转换全部属性 —————————————————————————————— //

  transAllProperty(DICT, tree) {
    tree.forEach(({ propertyCode, child }) => {
      if (child) {
        child.sort((a, b) => a.sortValue - b.sortValue)
        DICT[propertyCode] = child.map((item) => [
          item.propertyValue, // 0 主键
          item.propertyLabel, // 1 名称
          item.propertyCode, // 2 编号
          item.propertyAbout, // 3 备注
          item.parentId, // 4 父级
          () => item.enable === 2, // 5 状态
        ])
        this.transAllProperty(DICT, child)
      }
    })
    return DICT
  },

  // —————————————————————————————— 转换结构树 —————————————————————————————— //

  transTree(tree, parentId = '', parentName = '', parentIdList = [], parentNameList = []) {
    tree.forEach((item) => {
      const { children = [], id, name } = item
      const [idList, nameList] = [[...parentIdList, id], [...parentNameList, name]]
      Object.assign(item, { parentId, parentName, idList, nameList })
      this.transTree(children, id, name, idList, nameList)
    })
    return tree
  },

  // —————————————————————————————— 转换结构树映射 —————————————————————————————— //

  transTreeMap(treeMap, tree) {
    tree.forEach((item) => {
      treeMap[item.id] = item
      this.transTreeMap(treeMap, item.children || [])
    })
    return treeMap
  },

  // —————————————————————————————— 转换结构树字典 —————————————————————————————— //

  transTreeDict(treeDict, tree) {
    tree.forEach(({ children = [], id, name, describe }) => {
      const newChildren = []
      treeDict.push([newChildren, id, name, describe])
      this.transTreeDict(newChildren, children)
    })
    return treeDict
  },

  // —————————————————————————————— 转换全路径名称 —————————————————————————————— //

  transAllName(nameMap, treeMap, and = CORE.MAP.AND) {
    for (const id in treeMap) nameMap[id] = treeMap[id].nameList.join(and)
    return nameMap
  },

  // —————————————————————————————— 转换我的权限树 —————————————————————————————— //

  transMyTree(tree, idList) {
    const my = tree.filter(({ id }) => idList.includes(id))
    return my.map(({ children, ...other }) => ({ ...other, children: this.transMyTree(children, idList) }))
  },

  // —————————————————————————————— 转换我的权限字典 —————————————————————————————— //

  transMyDict(treeDict, idList) {
    const my = treeDict.filter(([, id]) => idList.includes(id))
    return my.map(([children, ...other]) => [this.transMyDict(children, idList), ...other])
  },

  // —————————————————————————————— 转换行政区 —————————————————————————————— //

  transDictrict(district) {
    const [PROVINCE, CITY, AREA] = [[], {}, {}]
    // 省份
    district.forEach((province) => {
      PROVINCE.push([province.id, province.name, province.describe])
      // 城市
      province.children.forEach((city) => {
        if (!CITY[province.id]) CITY[province.id] = []
        CITY[province.id].push([city.id, city.name, city.describe])
        // 区县
        city.children.forEach((area) => {
          if (!AREA[city.id]) AREA[city.id] = []
          AREA[city.id].push([area.id, area.name, area.describe])
        })
      })
    })
    return [PROVINCE, CITY, AREA]
  },

  // —————————————————————————————— 转换菜单树路由 —————————————————————————————— //

  transMenuRoute(menuRoute, tree) {
    tree.forEach((item) => {
      const { children = [], id, openType, catalogue } = item
      if ([1, 2].includes(openType)) menuRoute[catalogue] = item
      if ([3, 4].includes(openType)) menuRoute[id] = item
      this.transMenuRoute(menuRoute, children)
    })
    return menuRoute
  },

  // —————————————————————————————— 转换过滤菜单 —————————————————————————————— //

  transFilterMenu(menu) {
    const tree = menu.map((major) => {
      const children = (major.children || []).filter((minor) => (minor.children || []).length)
      return { ...major, children }
    })

    return tree.filter((major) => major.project === process.env.VUE_APP_PROJECT && major.children.length)
  },
}
